package com.cheng.example.juc;

import java.util.concurrent.CountDownLatch;

/*
 * CountDownLatch:闭锁，在完成某些运算时，只有其他所有线程的运算全部完成，当前运算才继续执行
 */
public class TestCountDownLatch {
    public static void main(String[] args) {
        final CountDownLatch latch=new CountDownLatch(10);
        LatchDemo ld=new LatchDemo(latch);

        long start=System.currentTimeMillis();

        for(int i=0;i<20;i++){
            new Thread(ld).start();
        }

        try {
            latch.await(); //直到50个人子线程都执行完，latch的值减到0时，才往下执行
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long end=System.currentTimeMillis();
        System.out.println("耗费时间为："+(end-start));
    }
}

class LatchDemo implements Runnable{
    private CountDownLatch latch;

    public LatchDemo(CountDownLatch latch){
        this.latch=latch;
    }
    @Override
    public void run() {
        try{
            for(int i=0;i<500;i++){
                if(i%2==0){
                    System.out.println(i);
                }
            }
        }finally{
            latch.countDown();//latch的值减一
        }
    }
}
